<template>
  <div class="container">
    <h1>{{id? '编辑':'添加'}}关于信息</h1>
    <el-form :model="model" @submit.native.prevent="save">
      <el-form-item label="昵称:">
        <el-input v-model="model.nickname"></el-input>
      </el-form-item>

      <el-form-item label="人生格言:">
        <el-input v-model="model.motto"></el-input>
      </el-form-item>

      <el-form-item label="联系方式:">
        <el-input v-model="model.contact[0]" placeholder="请输入CDSN链接"></el-input>
        <el-input v-model="model.contact[1]" placeholder="请输入微信"></el-input>
        <el-input v-model="model.contact[2]" placeholder="请输入邮箱"></el-input>
        <el-input v-model="model.contact[3]" placeholder="请输入qq"></el-input>
      </el-form-item>

      <el-form-item label="头像:">
        <el-input v-model="model.avatar" placeholder="请输入头像外部链接"></el-input>
        <el-avatar :size="100" fit="cover" :src="model.avatar" style="margin-top:20px"></el-avatar>
      </el-form-item>

      <el-form-item label="标签:">
        <el-input v-model="model.tag[0]"></el-input>
        <el-input v-model="model.tag[1]"></el-input>
        <el-input v-model="model.tag[2]"></el-input>
        <el-input v-model="model.tag[3]"></el-input>
        <el-input v-model="model.tag[4]"></el-input>
        <el-input v-model="model.tag[5]"></el-input>
        <el-input v-model="model.tag[6]"></el-input>
        <el-input v-model="model.tag[7]"></el-input>
        <el-input v-model="model.tag[8]"></el-input>
        <el-input v-model="model.tag[9]"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" native-type="submit">{{id? '保存':'添加'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    id: {},
  },
  data() {
    return {
      model: {
        nickname: "",
        motto: "",
        avatar: "",
        contact: [],
        tag: [],
      },
    };
  },
  methods: {
    async save() {
      // 添加个人信息
      if (!this.id) {
        let res = await this.$http.post("about", this.model);
        this.$message({
          type: "success",
          message: "添加个人信息成功",
        });
        this.$router.push("/about/list");
        // 编辑个人信息
      } else {
        let res = await this.$http.put(`/about/${this.id}`, this.model);
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.$router.push("/about/list");
      }
    },
    // 获取编辑个人信息初始数据
    async fetch() {
      const res = await this.$http.get(`/about/${this.id}`);
      this.model = res.data;
    },
  },
  created() {
    this.id && this.fetch();
  },
};
</script>

<style></style>